package itheima05;

import domain.User2;
import domain.Users;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import utils.C3P0Util;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xxx
 * @date 2021/1/5 23:55
 * @description
 */
public class TestDome {
    @Test
    public void  queryall() throws SQLException {
    //通过QueryRunner查询和BeanListHandler封装表内容放到集合User对象里面遍历出
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from  users";
        List<User2> list=queryRunner.query(C3P0Util.getConnection(),sql,new BeanListHandler<>(User2.class));
        for (User2 users : list) {
            System.out.println(users);
        }
        System.out.println("--------");
        //通过QueryRunner查询和BeanHandler封装表内容放到集合User对象里面遍历出
        User2 user2=queryRunner.query(C3P0Util.getConnection(),sql,new BeanHandler<User2>(User2.class));
        System.out.println(user2);
    //通过ScalarHandler(): 获取查询结果的第一行的第一列
         Object o=queryRunner.query(C3P0Util.getConnection(),sql, new ScalarHandler());
         System.out.println(o);

    // 通过ScalarHandler(): 获取查询结果的第一行的第？列
        Object o1=queryRunner.query(C3P0Util.getConnection(),sql,new ScalarHandler(3));
        System.out.println(o1);
    //通过ScalarHandler(): 根据字段名字获得第一行第几列
         Object o2=queryRunner.query(C3P0Util.getConnection(),sql,new ScalarHandler("upass"));
        System.out.println(o2);
    //ColumnListHandler(): 把第一列的内容存储到List集合中
         List<Object>list1=queryRunner.query(C3P0Util.getConnection(),sql,new ColumnListHandler());
        System.out.println(list1);
   //ColumnListHandler(): 把第？列的内容存储到List集合中
       List<Object>list2=queryRunner.query(C3P0Util.getConnection(),sql,new  ColumnListHandler(2));
        System.out.println(list2);
    //ColumnListHandler(): 根据字段名对应的内容内容存储到List集合中
        List<Object>list3=queryRunner.query(C3P0Util.getConnection(),sql,new ColumnListHandler("uname"));
        System.out.println(list3);
    }

}
